//
//  EquipmentRepairVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/9.
//  Copyright © 2020 tym. All rights reserved.
//

#import "EquipmentRepairVC.h"
#import "RepairDetailVC.h"
#import "Sizes.h"
#import "UserModel.h"
#import "FamilyModel.h"
#import "FamilyService.h"
#import "EquipmentRepairCell.h"
#import "GroupShadowTableView.h"
@interface EquipmentRepairVC ()<GroupShadowTableViewDelegate,GroupShadowTableViewDataSource>

@property(nonatomic,strong)  GroupShadowTableView *tableView;

@property(nonatomic,strong) FamilyService *familyService;
@property(nonatomic,strong) NSMutableArray *equipMents;
@end

@implementation EquipmentRepairVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"选择设备"];
    
    [self setTable];
    [self getEquipments];
}

-(FamilyService*)familyService{
    if (_familyService == nil) {
        _familyService = [[FamilyService alloc] init];
    }
    return _familyService;
}

-(NSMutableArray*)equipMents{
    if (_equipMents == nil) {
        _equipMents = [[NSMutableArray alloc] init];
    }
    return _equipMents;
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth, ScreenHeight-NavigateonHeight-74-TabbarHeight-10) style:UITableViewStyleGrouped];
    self.tableView.groupShadowDelegate = self;
    self.tableView.groupShadowDataSource = self;
    self.tableView.backgroundColor = viewBGColor;
    self.tableView.sectionFooterHeight = 0;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}

-(void)getEquipments{
    TYMWeak(self);
    [self.familyService getFamilyAllEquipmentWithFamilyId:[UserTool getCurrentFamily].familyId completion:^(NSError * _Nonnull error, NSArray<EquipmentModel *> * _Nonnull array) {
        if (weakself.equipMents.count) {
            [weakself.equipMents removeAllObjects];
        }
        [weakself.equipMents addObjectsFromArray:array];
        [self.tableView reloadData];
    }];
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return self.equipMents.count;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [EquipmentRepairCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    EquipmentRepairCell  *cell = [tableView dequeueReusableCellWithIdentifier:[EquipmentRepairCell identifier]];
    if (!cell) {
        cell = [[EquipmentRepairCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[EquipmentRepairCell identifier]];
    }
    [cell setEquipMentModel:self.equipMents[indexPath.section]];
    return cell;
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    RepairDetailVC *detailVC = [[RepairDetailVC alloc] initEquipMent:self.equipMents[indexPath.section]];
    [self.navigationController pushViewController:detailVC animated:YES];
}


@end
